using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using MbUnit.Framework;

namespace UnitTests
{
	[TestFixture]
	public class TestNAntProject
	{
		const string defaultNamespace = "http://nant.sf.net/nightly/2006-03-08-0.85/nant.xsd";

		[Test]
		public void TestTeo()
		{
			NAntProject project = new NAntProject();
			project.BaseDir = ".";
			project.DefaultTarget = "build";
			project.SetProperty("dosomething", "true");
			project.SetProperty("Radical", "yes!");

			project.Write("project.xml");
		}
		[Test]
		public void TestTree()
		{
			NAntProject project = new NAntProject("project.xml", "project", ".");
			project.DefaultTarget = "build";
			project.SetProperty("dosomething", "false");
			project.SetProperty("RadicalAndTrue", "yes!");

			project.Write("project.xml");
		}

		[Test]
		public void TestOne()
		{
			XmlDocument doc = new XmlDocument();

			string fileName = @"d:\src\Research\vstemplate\BuildTools\InstanceConfiguration\ProjectSettings.build.xml";
			doc.Load(fileName);
			XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
			manager.AddNamespace("x", defaultNamespace);

			XmlNode projectNode = doc.SelectSingleNode("/x:project", manager);

			XmlNode node = projectNode.SelectSingleNode("x:property[@name='IncludeDatabaseProject']", manager);
			Console.WriteLine(node.Attributes["name"].Value);
			XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "property", manager.LookupNamespace("x"));
			newNode.Attributes.Append(doc.CreateAttribute("name"));
			newNode.Attributes.Append(doc.CreateAttribute("value"));
			newNode.Attributes["name"].Value = "NewProperty";
			newNode.Attributes["value"].Value = "NewValue";
			projectNode.InsertAfter(newNode, node);
			WriteContent(doc, fileName);
		}

		private static void WriteContent(XmlDocument doc, string fileName)
		{
			XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName, Encoding.UTF8);
			xmlTextWriter.Formatting = Formatting.Indented;
			using (XmlWriter writer = xmlTextWriter)
				doc.WriteTo(writer);
		}

		XmlDocument CreateProjectDocument()
		{
			XmlDocument doc = new XmlDocument();
			XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
			manager.AddNamespace("x", defaultNamespace);
			XmlNode rootNode = doc.CreateNode(XmlNodeType.Element, "project", manager.LookupNamespace("x"));
			doc.AppendChild(rootNode);
			return doc;
		}
	}
}
